<template>
  <basic-container>
    <el-row :gutter="12">
      <!-- date遍历循环的数据 -->
      <el-col
        :span="6"
        class="el-col-cover"
        v-for="item in data"
        :key="item.id"
      >
        <el-card shadow="hover"
          ><!--style="background-color: #5daf34"  灰 #e1e1e1 绿 #5daf34-->
          <!-- 卡片的头部位 -->
          <template #header>
            <div class="card-header">
              <!-- 
                    这里声明一下,我在多选时,往数组中添加的是对象
                    label属性:是多选框的值,若该标签中无内容,则该属性也充当 checkbox 按钮后的介绍
                    @change:改变事件,多选框勾选和取消勾选都会触发事件,所以在取消勾选时要删除勾选状态下的值
                -->
              <el-checkbox
                v-model="checked"
                :label="item.id"
                @change="ids(item)"
                >{{ item.name }}</el-checkbox
              >
              <div>
                <!-- 修改按钮 -->
                <el-button
                  type="text"
                  icon="el-icon-edit-outline"
                  @click="handleUpdate(item)"
                />
                <!-- 删除按钮 -->
                <el-button
                  type="text"
                  icon="el-icon-delete"
                  @click="rowDel(item.id)"
                />
                <!-- 开关按钮
                <el-button
                  type="text"
                  icon="el-icon-switch-button"
                  @click="devicePowerBtn(item)"
                /> -->
              </div>
            </div>
          </template>
          <!-- 卡片显示的内容 -->
          <div class="content">
            <img :src="item.url" class="content-img" />
            <div class="content-info">
              <span>价格：{{ item.price }}￥</span> <br />
              <span>数量：{{ item.num }}</span>
            </div>
            <el-button class="content-join" style="width: 100px"
              >加入购物车</el-button
            >
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog title="修改零件信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="零件名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="零件价格" :label-width="formLabelWidth">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="零件数量" :label-width="formLabelWidth">
          <el-input v-model="form.num" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分页 -->
    <div class="blockPage">
      <div style="margin-top: 0.2rem; margin-left: 0.2rem">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 7, 10]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totals"
        >
        </el-pagination>
      </div>
    </div>
  </basic-container>
</template>

<script>
import axios from "axios";
import { onMounted, beforeCreate } from "vue";
export default {
  data() {
    return {
      form: {
        name: "",
        price: "",
        num: "",
      },
      currentPage: 1,
      pagesize: 10,
      name: "",
      totals: 10,
      data: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      partId: "",
      checked:''
    };
  },
  mounted: function () {
    this.onLoad(1, 10);
  },

  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(`每页 ${val} 条`);
      this.onLoad(this.currentPage, val);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      console.log("this.currentPage=" + this.currentPage);
      console.log("this.pagesize=" + this.pagesize);
      this.onLoad(this.currentPage, this.pagesize);
    },

    //分页接口
    onLoad(current, size) {
      axios({
        method: "post",
        url: "http://localhost:9090/part/getPartByPage",
        data: {
          current: current,
          size: size,
        },
      })
        .then((res) => {
          console.log(res);
          console.log("分页查询partImgUrl成功，data:", res.data);
          this.data = res.data.records;
          this.totals = res.data.total;
          console.log("totals:=" + this.totals);
        })
        .catch((err) => {
          console.log(err);
          console.log("addPart");
        });
    },

    //删除零件
    rowDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          axios({
            method: "post",
            url: "http://localhost:9090/part/delete",
            data: {
              id: id,
            },
          })
            .then((res) => {
              console.log("删除成功");
              this.data.forEach((item, index, arr) => {
                if (item.id == id) {
                  arr.splice(index, 1);
                }
              });
            })
            .catch((err) => {
              console.log("删除失败---" + err);
            });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    handleUpdate(item) {
      this.dialogFormVisible = true;
      this.partId = item.id;
      console.log("---------------item:",item)
    },

    handleConfirm() {
      console.log("============item:", this.partId);

      axios({
        method: "post",
        url: "http://localhost:9090/part/update",
        data: {
          id: this.partId,
          name: this.form.name,
          price: this.form.price,
          num: this.form.num,
        },
      })
        .then((res) => {
          this.data.forEach((part,index,arr)=>{
              if(this.partId==part.id){
                part.name=this.form.name,
                part.price=this.form.price,
                part.num=this.form.num
                arr[index]=part;
              }
          })
          console.log("修改成功");
          this.dialogFormVisible=false;
          this.$message({
            message:"零件编辑成功",
            type:"success"
          })
        })
        .catch((err) => {
          this.dialogFormVisible=false;
          this.$message.error("零件编辑失败")
          console.log("修改失败---" + err);
        });
    },
  },
};
</script>
<style>
.content {
  display: flex;
  flex-direction: column;
}
.el-col-cover {
  width: 20%;
}
.content-join {
  margin-left: 150px;
  background-color: #ea791e;
  color: white;
}
</style>
